//
//  ACEStudentDetailListController.m
//  ACE
//
//  Created by zhangtao on 2018/8/9.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "ACEStudentDetailListController.h"
#import "ACEStudentDetailListTopView.h"
#import "ACEStudentDetailListHeaderView.h"
#import "ACEStudentDetailListFooterView.h"
#import "ACEStudentDetailListDesCell.h"
#import "ACEStudentDetailListCommentCell.h"
#import "ACEStudentCourseFileController.h"
#import "ACEStudentDetailListCommentController.h"
#import "ACETStudentDetailListDateController.h"
@interface ACEStudentDetailListController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *table;
@property(nonatomic,strong)NSMutableArray *stateArray;

#define topView_H 50
@end

@implementation ACEStudentDetailListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"课程文件";
    _stateArray = [NSMutableArray arrayWithObjects:@"1",@"1",@"1", nil];
    [self createUI];
}

-(void)createUI{
    if (!_table) {
        ACEStudentDetailListTopView *topView = [[ACEStudentDetailListTopView alloc]initWithFrame:CGRectMake(10, navHeight+5, ACEWidth-20, topView_H)];
        topView.studentDetailListTopClickCallBack = ^{
            ACEStudentCourseFileController *file = [[ACEStudentCourseFileController alloc]init];
            [self.navigationController pushViewController:file animated:YES];
        };
        [self.view addSubview:topView];

        _table = [[UITableView alloc]initWithFrame:CGRectMake(5, navHeight+topView_H+10, ACEWidth-10, ACEHeight) style:UITableViewStyleGrouped];
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;

        [self.view addSubview:_table];
        _table.delegate = self;
        _table.dataSource = self;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 200;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return topView_H;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    ACEStudentDetailListFooterView *footer = [[ACEStudentDetailListFooterView alloc]initWithFrame:CGRectMake(0, 0, self.table.frame.size.width, topView_H)];
    footer.studentDetailListFooterClickCallBack = ^(student_detail_footer type) {
        if (type==student_detail_list_comment) {
            //评论
            
            ACEStudentDetailListCommentController *comment = [[ACEStudentDetailListCommentController alloc]initWithNibName:@"ACEStudentDetailListCommentController" bundle:nil];
            [self.navigationController pushViewController:comment animated:YES];
        }else{
            //日期
            ACETStudentDetailListDateController *date = [[ACETStudentDetailListDateController alloc]init];
            [self.navigationController pushViewController:date animated:YES];
        }
    };
    return footer;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ACEStudentDetailListHeaderView *head = [[ACEStudentDetailListHeaderView alloc]initWithFrame:CGRectMake(0, 0, self.table.frame.size.width, 200)];
    head.starNum = 4;
    head.showState = _stateArray[section];
//    __block typeof(table) blockTable = table;
    head.DetailListSoowStateCallBack = ^(NSInteger state) {
        if (state == 2) {
            [self.stateArray replaceObjectAtIndex:section withObject:@"2"];
        }else{
            [self.stateArray replaceObjectAtIndex:section withObject:@"1"];
        }
        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];
        [self.table reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];

    };

    return head;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([_stateArray[section] isEqualToString:@"1"]) { //学生评价
        return 3;
    }else{
        return 1;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _stateArray.count;
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
        return 50;
    }
    return 100;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *desIdentifier = @"desCell";
    static NSString *commIdentifier = @"commCell";
    static NSString *moreComment = @"moreCommentCell";
    if ([_stateArray[indexPath.section] isEqualToString:@"1"]) {
        
        if (indexPath.row == 2) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:moreComment];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:moreComment];
                UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(self.table.frame.size.width/2-50, 0, 100, 50)];
                [button setTitle:@"更多" forState:UIControlStateNormal];
                [button setImage:[UIImage imageNamed:@"下啦"] forState:UIControlStateNormal];
                [button setTitleColor:deepGreen forState:UIControlStateNormal];
                [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 40)];
                [button setImageEdgeInsets:UIEdgeInsetsMake(0, 55, 0, 0)];
                [cell addSubview:button];
                [button setTag:indexPath.section];
                [button addTarget:self action:@selector(moreComment:) forControlEvents:UIControlEventTouchUpInside];
            }
            return cell;

        }
        
        ACEStudentDetailListCommentCell *cell = [[ACEStudentDetailListCommentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:commIdentifier];//WithFrame:CGRectMake(0, 0, _table.frame.size.width, 60)];
        cell.starView.starNum = 3;
//        [cell topRound];
        return cell;
    }else{
        ACEStudentDetailListDesCell *cell = [[ACEStudentDetailListDesCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:desIdentifier];//WithFrame:CGRectMake(0, 0, _table.frame.size.width, 60)];
        return cell;
    }
}

-(void)moreComment:(UIButton *)btn{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
